﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gpsys.Entity.MSG;
using Gpsys.Logger;

namespace Gpsys.Web.Business
{
    /// <summary>
    /// WorkflowBusiness
    /// </summary>
    public class WorkflowBusiness : BaseBusiness
    {
        /// <summary>
        /// Updates the workflow.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        /// <returns></returns>
        public bool UpdateWorkflow(Workflow workflow)
        {
            try
            {
                SQL.OpenConnection();

                SQL.Execute(@"UPDATE [Workflow]
                                               SET [Code] = @code
                                                  ,[Description] = @description
                                                  ,[Xml] = @xml
                                                  ,[ModifiedBy] = @modifiedBy
                                                  ,[ModifiedOn] = getdate()
                                             WHERE [Id] = @Id",
                                                         new
                                                         {
                                                             Id = workflow.Id,
                                                             code = workflow.Code,
                                                             description = workflow.Description,
                                                             xml = workflow.Xml,
                                                             modifiedBy = workflow.ModifiedBy
                                                         });

            }
            catch (Exception e)
            {
                Log.WriteError("UpdateWorkflow : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }

            return true;
        }

        public bool DeleteWorkflow(int Id)
        {
            try
            {
                SQL.OpenConnection();

                SQL.Execute(@"UPDATE [Workflow]
                                               SET IsDeleted =  @IsDeleted
                                             WHERE [Id] = @Id",
                                                         new
                                                         {
                                                             Id = Id,
                                                             IsDeleted = true
                                                         });

            }
            catch (Exception e)
            {
                Log.WriteError("DeleteWorkflow : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }

            return true;
        }

        /// <summary>
        /// Gets the worflows.
        /// </summary>
        /// <returns></returns>
        public List<Workflow> GetWorflows()
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Workflow>(@"SELECT [Id]
                                                  ,[Code]
                                                  ,[Description]
                                                  ,[Xml]
                                                  ,[CreatedBy]
                                                  ,[CreatedOn]
                                                  ,[ModifiedBy]
                                                  ,[ModifiedOn]
                                                  ,[IsDeleted]
                                              FROM [Workflow]
                                            Where IsDeleted = 0").ToList();

            }
            catch (Exception e)
            {
                Log.WriteError("GetWorflows : " + e.Message);

                return new List<Workflow>();
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

        /// <summary>
        /// Gets the workflow.
        /// </summary>
        /// <param name="Id">The id.</param>
        /// <returns></returns>
        public Workflow GetWorkflow(int Id)
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Workflow>(@"SELECT [Id]
                                                  ,[Code]
                                                  ,[Description]
                                                  ,[Xml]
                                                  ,[CreatedBy]
                                                  ,[CreatedOn]
                                                  ,[ModifiedBy]
                                                  ,[ModifiedOn]
                                                  ,[IsDeleted]
                                              FROM [Workflow]
                                              WHERE [Id] = @id", new { id = Id }).SingleOrDefault();

            }
            catch (Exception e)
            {
                Log.WriteError("GetWorflow : " + e.Message);

                return null;
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

        /// <summary>
        /// Gets the workflow.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <returns></returns>
        public Workflow GetWorkflow(string code)
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<Workflow>(@"SELECT [Id]
                                                  ,[Code]
                                                  ,[Description]
                                                  ,[Xml]
                                                  ,[CreatedBy]
                                                  ,[CreatedOn]
                                                  ,[ModifiedBy]
                                                  ,[ModifiedOn]
                                                  ,[IsDeleted]
                                              FROM [Workflow]
                                              WHERE [Code] = @code", new { code = code }).SingleOrDefault();

            }
            catch (Exception e)
            {
                Log.WriteError("GetWorflow : " + e.Message);

                return null;
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

        /// <summary>
        /// Inserts the workflow.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        /// <returns></returns>
        public bool InsertWorkflow(Workflow workflow)
        {
            try
            {
                SQL.OpenConnection();

                workflow.Id = SQL.Query<int>(@"INSERT INTO [Workflow]
                               ([Code]
                               ,[Description]
                               ,[Xml]
                               ,[CreatedBy]
                               ,[CreatedOn]
                               ,[ModifiedBy]
                               ,[ModifiedOn]
                               ,[IsDeleted])
                         VALUES
                               ( @code,
                                 @description,
                                 @xml,
                                 @createdBy,
                                 getdate(),
                                 @modifiedBy,
                                 getdate(),
                                 0);

                        SELECT CAST(SCOPE_IDENTITY() as int);",
                                                         new
                                                            {
                                                                code = workflow.Code,
                                                                description = workflow.Description,
                                                                xml = workflow.Xml,
                                                                createdBy = workflow.CreatedBy,
                                                                modifiedBy = workflow.ModifiedBy
                                                            }).SingleOrDefault();

            }
            catch (Exception e)
            {
                Log.WriteError("InsertWorkflow : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }

            return true;
        }


        /// <summary>
        /// Checks the code used.
        /// </summary>
        /// <param name="workflow">The workflow.</param>
        /// <returns></returns>
        public bool CheckCodeUsed(Workflow workflow)
        {
            try
            {
                SQL.OpenConnection();

                return SQL.Query<int>(@"Select count(1) from [Workflow] where [Code] = @code and [Id] != @Id",
                                                         new
                                                         {
                                                             Id = workflow.Id,
                                                             code = workflow.Code
                                                         }).SingleOrDefault() > 0;

            }
            catch (Exception e)
            {
                Log.WriteError("CheckCodeUsed : " + e.Message);

                return false;
            }
            finally
            {
                SQL.CloseConnection();
            }
        }

    }
}
